//
//  XZMyGroupVC.m
//  Teecloud
//
//  Created by PC on 2018/10/25.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZMyGroupVC.h"
#import "GMGroupModel.h"
#import "GMGroupCell.h"
#import "XZMyGroupDetailVC.h"
#import "UIViewController+CYLTabBarControllerExtention.h"
#import "UIBarButtonItem+GMBarButtonitem.h"
@interface XZMyGroupVC ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tabView;
@property (nonatomic, assign) NSInteger pageIndex;
//第一次进来加载菊花 后面的不加载
@property (nonatomic, assign) BOOL isFirst;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

#define GROUPCELL  @"GMGroupCell"
@implementation XZMyGroupVC
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getMyGroupList];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的班组社团";
    self.view.backgroundColor = kWhiteColor;
    if (self.isJoinVC) {
        [self setleftBarButtonItems];
    }
}

/**
 重写返回按钮 当申请加入成功跳转到此页面的时候 返回到首页
 */
- (void)setleftBarButtonItems{
    UIBarButtonItem *backBarButtonItem = [UIBarButtonItem backItemWithImage:[UIImage imageNamed:@"left-white-64"] WithHighlightedImage:[UIImage imageNamed:@"left-gray-64"] Target:self action:@selector(bakcHome) title:@""];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = kIs_iPhoneX ? -12 : -8;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, backBarButtonItem, nil];
}
//回到首页
- (void)bakcHome{
    [self cyl_popSelectTabBarChildViewControllerAtIndex:0 completion:^(__kindof UIViewController *selectedTabBarChildViewController) {
    }];
}
#pragma mark - MJRefresh
- (void)getMyGroupList{
    self.pageIndex = 0;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    [dict setValue:@(self.pageIndex) forKey:@"pageIndex"];
    if (!self.isFirst) {
        self.isFirst = YES;
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    }
    kWeakObject(self)
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"ajax-api.ashx?$c=TeeyunApp&$m=GetMyGroupList&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [weakself.tabView.mj_header endRefreshing];
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                [weakself.dataSource removeAllObjects];
                NSArray *array = [GMGroupModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:RESULT_NAME_Data]];
                [weakself.dataSource addObjectsFromArray:array];
                [weakself.tabView.mj_footer endRefreshing];
                [weakself.tabView.mj_header endRefreshing];
                if (weakself.dataSource.count == 0) {
                    [weakself.tabView setupEmptyDataText:@"暂无数据" secoundText:@"" buttonTit:@"" verticalOffset:0 emptyImage:NODATAIMG tapBlock:^{
                        
                    } butClick:^{
                        
                    }];
                }
                [weakself.tabView reloadData];
            }else{
                
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakself.view];
            }
        }else{
            [MBProgressHUD showErrorOrSuc:@"网络异常" toView:weakself.view];
        }
    }];
}
- (void)getMoreMyGroupList{
    self.pageIndex ++;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:FROM_IOS forKey:@"from"];
    [dict setValue:[XGMArchiverTools cacheForKey:BUSIORGID] forKey:@"busiOrgId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_UserId] forKey:@"userId"];
    [dict setValue:[XGMArchiverTools cacheForKey:USER_identifierNumber] forKey:@"loginFinger"];
    [dict setValue:@(self.pageIndex) forKey:@"pageIndex"];
    
    kWeakObject(self)
    [HJNetwork POSTWithURL:[NSString stringWithFormat:@"ajax-api.ashx?$c=TeeyunApp&$m=GetMyGroupList&$v=%@",VERSION] parameters:dict cachePolicy:HJCachePolicyIgnoreCache callback:^(id responseObject, NSError *error) {
        [MBProgressHUD hideHUDForView:weakself.view animated:YES];
        if (!error) {
            if ([[responseObject objectForKey:RESULT_NAME_Success] boolValue]) {
                NSArray *dataInfo = [responseObject objectForKey:RESULT_NAME_Data];
                NSArray *newsArray = [GMGroupModel mj_objectArrayWithKeyValuesArray:dataInfo];
                [weakself.dataSource addObjectsFromArray:newsArray];
                if (dataInfo.count>0) {
                    [weakself.tabView.mj_footer endRefreshing];
                    [weakself.tabView.mj_header endRefreshing];
                    [weakself.tabView reloadData];
                }else{//没有数据的时候就显示暂无数据
                    weakself.tabView.mj_footer.state = MJRefreshStateNoMoreData;
                }
                
            }else{
                [weakself.tabView.mj_footer endRefreshing];
                [weakself.tabView.mj_header endRefreshing];
                [MBProgressHUD showErrorOrSuc:[responseObject objectForKey:RESULT_NAME_Message] toView:weakself.view];
            }
        }else{
            [weakself.tabView.mj_footer endRefreshing];
            [weakself.tabView.mj_header endRefreshing];
              [MBProgressHUD showErrorOrSuc:@"网络异常" toView:weakself.view];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    self.tabView.mj_footer.hidden = self.dataSource.count > 0? NO:YES;
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GMGroupCell *cell = [tableView dequeueReusableCellWithIdentifier:GROUPCELL];
    GMGroupModel *model = self.dataSource[indexPath.row];
    cell.model = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80.f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    GMGroupModel *model = self.dataSource[indexPath.row];
    XZMyGroupDetailVC *groupDetailVC = [[XZMyGroupDetailVC alloc]init];
    groupDetailVC.titleStr = model.Name;
    groupDetailVC.groupId = model.Id;
    [self.navigationController pushViewController:groupDetailVC animated:true];
}
#pragma mark - setter
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource =[[NSMutableArray alloc]init];
    }
    return _dataSource;
}
- (UITableView *)tabView{
    if (!_tabView) {
        _tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT-kNavBarAndStatusBarHeight-kBottomSafeHeight) style:UITableViewStylePlain];
        _tabView.delegate = self;
        _tabView.dataSource = self;
        _tabView.showsVerticalScrollIndicator =false;
        _tabView.separatorStyle =UITableViewCellSeparatorStyleNone;
        [_tabView registerNib:[UINib nibWithNibName:@"GMGroupCell" bundle:nil] forCellReuseIdentifier:GROUPCELL];
        
        //上下拉刷新
        _tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getMyGroupList)];
        _tabView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreMyGroupList)];
        _tabView.mj_footer.hidden =YES;
        [self.view addSubview:_tabView];
    }
    return _tabView;
}
@end
